package com.guigu.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.base.entity.Truck;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.base.mapper.TruckMapper;
import com.guigu.base.service.TruckService;
import com.guigu.common.utils.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 车辆信息表 服务实现类
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-08
 */
@Service
public class TruckServiceImpl extends ServiceImpl<TruckMapper, Truck> implements TruckService {

    /**
     * 添加车辆
     *
     * @param pdTruck 车辆信息
     * @return 车辆信息
     */
    @Override
    public Truck saveTruck(Truck pdTruck) {
        baseMapper.insert(pdTruck);
        return pdTruck;
    }

    /**
     * 获取车辆分页数据
     *
     * @param page         页码
     * @param pageSize     页尺寸
     * @param truckTypeId  车辆类型id
     * @param licensePlate 车辆号牌
     * @param fleetId
     * @return 线路类型分页数据
     */
    @Override
    public IPage<Truck> findByPage(Integer page, Integer pageSize, String truckTypeId, String licensePlate, String fleetId) {
        Page<Truck> iPage = new Page(page, pageSize);
        LambdaQueryWrapper<Truck> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(licensePlate)) {
            lambdaQueryWrapper.like(Truck::getLicensePlate, licensePlate);
        }
        if (StringUtils.isNotEmpty(truckTypeId)) {
            lambdaQueryWrapper.eq(Truck::getTruckTypeId, truckTypeId);

        }
        if (StringUtils.isNotEmpty(fleetId)) {
            lambdaQueryWrapper.eq(Truck::getFleetId, fleetId);

        }
        lambdaQueryWrapper.eq(Truck::getStatus, Constant.DATA_DEFAULT_STATUS);
        lambdaQueryWrapper.orderBy(true, false, Truck::getId);
        return baseMapper.selectPage(iPage, lambdaQueryWrapper);
    }

    /**
     * 获取车辆列表
     *
     * @param ids     车辆id列表
     * @param fleetId 车队id
     * @return 车辆列表
     */
    @Override
    public List<Truck> findAll(List<String> ids, String fleetId) {
        LambdaQueryWrapper<Truck> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ids != null && ids.size() > 0) {
            lambdaQueryWrapper.in(Truck::getId, ids);
        }
        if (StringUtils.isNotEmpty(fleetId)) {
            lambdaQueryWrapper.eq(Truck::getFleetId, fleetId);
        }
        lambdaQueryWrapper.eq(Truck::getStatus, Constant.DATA_DEFAULT_STATUS);
        lambdaQueryWrapper.orderBy(true, false, Truck::getId);
        return baseMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 统计车辆数量
     *
     * @param fleetId 车队id
     * @return 车辆数量
     */
    @Override
    public Integer count(String fleetId) {
        LambdaQueryWrapper<Truck> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(fleetId)) {
            lambdaQueryWrapper.eq(Truck::getId, fleetId);
        }
        lambdaQueryWrapper.eq(Truck::getStatus, Constant.DATA_DEFAULT_STATUS);
        return baseMapper.selectCount(lambdaQueryWrapper);
    }

    /**
     * 删除车辆
     *
     * @param id
     */
    @Override
    public void disableById(String id) {
        Truck pdTruck = new Truck();
        pdTruck.setId(id);
        pdTruck.setStatus(Constant.DATA_DISABLE_STATUS);
        baseMapper.updateById(pdTruck);
    }
}
